home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / x / volume2 / widgetwrap / part01 next >
Encoding:
Internet Message Format  |  1993-04-28  |  13.2 KB

  1. From: mikew@wyse.wyse.com (Mike Wexler)
  2. Newsgroups: comp.sources.x
  3. Subject: v02i005:  a different interface to widget creation, Part01/01
  4. Message-ID: <1797@wyse.wyse.com>
  5. Date: 20 Oct 88 18:33:06 GMT
  6. Approved: mikew@wyse.com
  7.  
  8. Submitted-by: Dan Heller <island!maui!argv@Sun.COM>
  9. Posting-number: Volume 2, Issue 5
  10. Archive-name: widgetwrap/part01
  11.  
  12.  
  13.  
  14. Mike -- I think the net will find this useful ... Please ACK this note.
  15.  
  16. : This is a shell archive file. Remove everything above this line
  17. : to unbundle. chmod +x "thisfile", then run it: e.g. % thisfile
  18. : SHAR archive format.  Archive created Sat Oct 15 19:55:54 PDT 1988
  19. : file contains: 
  20. :    README
  21. :    WidgetWrap.man
  22. :    WidgetWrap.c
  23. :    WidgetWrap.h
  24. :    Patchlevel.h
  25. echo x - README
  26. sed 's/^X//' > README <<'+END+OF+README'
  27. X
  28. XThis software was written by Dan Heller <island!argv@sun.com> or
  29. X<dheller@ucbcory.berkeley.edu>.  Mail to me if you have comments,
  30. Xsuggestions, bugs, or whatever.
  31. X
  32. XThis is the WidgetWrap library.  It contains four files:
  33. X    WidgetWrap.c
  34. X    WidgetWrap.h
  35. X    WidgetWrap.man
  36. X    README
  37. X
  38. XWidgetWrap.c -- variable argument style programmer interfaces to widgets:
  39. X    WidgetCreate(name, class, parent, varargs...);
  40. X    WidgetSet(name, varargs);
  41. X    WidgetGet(name, varargs);
  42. X    GenericWidgetName(buf);
  43. X
  44. XThe purpose of this library is to allow the programmer to create
  45. Xwidgets and set/get widget attributes via a variable argument list
  46. Xstyle of function call.  This eliminates the need for many local
  47. Xvariables and bothersome XtSetArg() calls and so forth.  An example
  48. Xof usage:
  49. X
  50. X    Widget foo;
  51. X
  52. X    foo = WidgetCreate("foo", labelWidgetClass, toplevel,
  53. X        XtNlabel,    "Widget",
  54. X    XtNforeground,    WhitePixelOfScreen(XtScreen(toplevel)),
  55. X    XtNbackground,    BlackPixelOfScreen(XtScreen(toplevel)),
  56. X    XtNborderWidth,    1,
  57. X    NULL);
  58. X
  59. XSee the C code and man page for more extensive documentation and notes.
  60. X
  61. XTo compile, just cc -c WidgetWrap.c and append the object file to any
  62. Xlocal libraries you may have.  Or, just use the .o to link with other
  63. Xprograms you may be writing.  Install the WidgetWrap.h file somewhere
  64. Xlike /usr/include/X11 so you don't have to add annoying -I options to
  65. Xyour compile command line or makefiles.
  66. +END+OF+README
  67. echo '-rw-rw-r--  1 argv         1414 Oct 15 19:53 README    (as sent)'
  68. chmod u=rw,g=rw,o=r README
  69. ls -l README
  70. echo x - WidgetWrap.man
  71. sed 's/^X//' > WidgetWrap.man <<'+END+OF+WidgetWrap.man'
  72. X.\" This man page was written by Dan Heller <island!argv@sun.com>
  73. X.\" or <dheller@ucbcory.berkeley.edu>
  74. X.TH WidgetWrap 3X "" "1 March 1988" "X Version 11" 
  75. X.SH NAME
  76. XWidgetCreate, WidgetSet, WidgetGet \- programmer's convenience routines
  77. X.SH SYNTAX
  78. XWidgetCreate\^(\fIname\fP\^, \fIclass\fP\^, \fIparent\fP\^, \fIvarargs\fP)
  79. X.br
  80. X      char *\fIname\fP\^;
  81. X.br
  82. X      WidgetClass \fIclass\fP\^;
  83. X.br
  84. X      Widget \fIparent\fP\^;
  85. X.LP
  86. XWidgetSet\^(\^\fIwidget\fP, \fIvarargs\fP\^)
  87. X.br
  88. X      Widget *\fIwidget\fP\^;
  89. X.LP
  90. XWidgetGet\^(\^\fIwidget\fP, \fIvarargs\fP\^)
  91. X.br
  92. X      Widget *\fIwidget\fP\^;
  93. X.LP
  94. Xchar *
  95. XGenericWidgetName\^(\^\fIbuf\fP)
  96. X.br
  97. X      char *\fIbuf\fP\^;
  98. X.SH ARGUMENTS
  99. X.IP \fIbuf\fP 1i
  100. XBuffer to place new widget name.
  101. X.IP \fIclass\fP 1i
  102. XThe class of the widget to be created.
  103. X.IP \fIname\fP 1i
  104. XThe name of the widget when created.
  105. X.IP \fIparent\fP 1i
  106. XThe parent of the widget to be created.
  107. X.IP \fIwidget\fP 1i
  108. XThe widget to set or get attributes.
  109. X.SH DESCRIPTION
  110. X.ds WC .PN WidgetCreate
  111. XThe purpose of these functions is to allow the programmer to create
  112. Xwidgets and set and get widget attributes via a variable argument list
  113. Xstyle of function call.  This eliminates the need for many local
  114. Xvariables and bothersome XtSetArg() calls and so forth.  An example
  115. Xof usage:
  116. X.sp
  117. X.in +2
  118. X.nf
  119. XWidget foo;
  120. X.sp
  121. Xfoo = WidgetCreate("foo", labelWidgetClass, toplevel,
  122. X.in +2
  123. XXtNlabel,           "Widget",
  124. XXtNforeground,      WhitePixelOfScreen(XtScreen(toplevel)),
  125. XXtNbackground,      BlackPixelOfScreen(XtScreen(toplevel)),
  126. XXtNborderWidth,     1,
  127. XNULL);
  128. X.sp
  129. X.fi
  130. X.in -4
  131. XAs you can see, the list must be NULL terminated.  You may pass up to
  132. Xto MAXARGS argument pairs (which is defined in WidgetWrap.h).  There are
  133. Xspecial args available to the Create/Get/Set functions that are available:
  134. X.sp
  135. X.in +2
  136. XXtNmanaged\ \ \ \ pass "False" to create a non-managed widget.
  137. XXtNargList\ \ \ \ takes \fItwo\fP parameters.
  138. X.in -2
  139. X.sp
  140. XThe XtNargList makes it possible to pass attributes to the Create/Get/Set
  141. Xcalls that are probably common to many widgets to be created or reset.
  142. X.sp
  143. X.in +2
  144. X.nf
  145. Xstatic Arg args[] = {
  146. X.in +2
  147. XXtNforeground,   black,
  148. XXtNbackground,   white,
  149. XXtNwidth,        20,
  150. XXtNheight,       10,
  151. X.ti -2
  152. X};
  153. X.sp
  154. X.ti -2
  155. Xfoo = WidgetCreate(NULL, widgetClass, toplevel,
  156. XXtNargList,      args, XtNumber(args),
  157. XNULL);
  158. X.in -4
  159. X.fi
  160. X.sp
  161. XMost large applications will create huge numbers of widgets which the
  162. Xprogrammer has to think up unique names for all of them.  What's more,
  163. Xtypically, as noted by the examples above, the names are constant strings
  164. Xwhich takes up memory, disk spaces, etc...  So, if WidgetCreate() gets
  165. XNULL as the name of the widget, then a widget name will be created
  166. Xautomatically by calling GenericWidgetName() since most of the time,
  167. Xuser's don't care what the name of a widget is, this capability is available.
  168. X.SH DIAGNOSTICS
  169. XWidgetCreate() will return NULL if a widget cannot be created.  It uses
  170. Xthe routines (and is a front end for) XtCreateWidget() and
  171. XXtCreateManagedWidget().
  172. X.SH "SEE ALSO"
  173. XXtCreateWidget(3X),
  174. XXtCreateManagedWidget(3X),
  175. X.br
  176. X\fIXlib \- C Language X Interface\fP
  177. +END+OF+WidgetWrap.man
  178. echo '-rw-rw-r--  1 argv         3098 Oct 15 19:48 WidgetWrap.man    (as sent)'
  179. chmod u=rw,g=rw,o=r WidgetWrap.man
  180. ls -l WidgetWrap.man
  181. echo x - WidgetWrap.c
  182. sed 's/^X//' > WidgetWrap.c <<'+END+OF+WidgetWrap.c'
  183. X/*
  184. X * WidgetWrap.c -- variable argument style programmer interfaces to widgets:
  185. X *    WidgetCreate(name, class, parent, varargs...);
  186. X *    WidgetSet(name, varargs);
  187. X *    WidgetGet(name, varargs);
  188. X *    GenericWidgetName(buf);
  189. X *
  190. X * This module was written by Dan Heller <island!argv@sun.com> or
  191. X * <dheller@ucbcory.berkeley.edu>.
  192. X *
  193. X * The purpose of this module is to allow the programmer to Create
  194. X * widgets and set/get widget attributes via a variable argument list
  195. X * style of function call.  This eliminates the need for many local
  196. X * variables and bothersome XtSetArg() calls and so forth.  An example
  197. X * of usage:
  198. X *
  199. X *    Widget foo;
  200. X *
  201. X *    foo = WidgetCreate("foo", labelWidgetClass, toplevel,
  202. X *        XtNlabel,        "Widget",
  203. X *      XtNforeground,    WhitePixelOfScreen(XtScreen(toplevel)),
  204. X *      XtNbackground,    BlackPixelOfScreen(XtScreen(toplevel)),
  205. X *        XtNborderWidth,    1,
  206. X *        NULL);
  207. X *
  208. X * As you can see, the list must be NULL terminated.  You may pass up to
  209. X * to MAXARGS argument pairs.  Increase this number in WidgetWrap.h if
  210. X * necessary.  There are special args availabel to the create/get/set
  211. X * functions that are available:
  212. X *
  213. X *    XtNmanaged        pass "False" to create a non-managed widget.
  214. X *    XtNargList        takes _two_ parameters.
  215. X *
  216. X * The XtNargList makes it possible to pass attributes to the create/get/set
  217. X * calls that are probably common to many widgets to be created or reset.
  218. X *
  219. X * static Arg args[] = {
  220. X *     XtNforeground,    black,
  221. X *     XtNbackground,   white,
  222. X *     XtNwidth,    20,
  223. X *     XtNheight,    10,
  224. X * };
  225. X * foo = WidgetCreate("bar", widgetClass, toplevel,
  226. X *     XtNargList,    args, XtNumber(args),
  227. X *     NULL);
  228. X *
  229. X * Most large applciations will create huge numbers of widgets which the
  230. X * programmer has to think up unique names for all of them.  What's more,
  231. X * typically, as noted by the examples above, the names are constant strings
  232. X * which takes up memory, disk spaces, etc...  So, if WidgetCreate() gets
  233. X * NULL as the name of the widget, then a widget name will be created
  234. X * automatically.  Since most of the time, user's don't care what the name
  235. X * of a widget is, this capability is available.
  236. X *
  237. X * Finally, a note about varargs.  Note that there are many different
  238. X * implementations of varargs.  To maintain portability, it is important
  239. X * to never return from a function that uses varargs without calling va_end().
  240. X * va_start() and va_end() should always exist in the same block of {}'s.
  241. X * There can be blocks between them, but va_end() shouldn't be in a block
  242. X * inside of the va_start() stuff.  This is to allow support for weird
  243. X * implementations which define va_start() to something like:  ".... { "
  244. X * (pyramid computers for one).
  245. X * Also, if you use varargs, never declare "known" arguments; extract them
  246. X * from the vararg list later.
  247. X */
  248. X#include <stdio.h>
  249. X#include <X11/Intrinsic.h>
  250. X#include <varargs.h>
  251. X#include "X11/WidgetWrap.h"
  252. X
  253. Xchar *
  254. XGenericWidgetName(buf)
  255. Xchar *buf;
  256. X{
  257. X    static int widget_count;
  258. X
  259. X    (void) sprintf(buf, "_widget.%6d", widget_count++);
  260. X    return buf;
  261. X}
  262. X
  263. X/*
  264. X * WidgetCreate()
  265. X *    Create a widget passing it's instance attributes and other parameters
  266. X * as variable arguments.  This removes the need to build your own Arg[]
  267. X * lists, etc...  Terminate argument list pairs with a NULL argument.
  268. X */
  269. X/* VARARGS */
  270. XWidget
  271. XWidgetCreate(va_alist)
  272. Xva_dcl
  273. X{
  274. X    va_list    var;
  275. X    Arg        args[MAXARGS];
  276. X    int        err = 0, nargs, i = 0;
  277. X    int        managed = True;
  278. X    String    argstr;
  279. X    XtArgVal    argval;
  280. X    char    *name, buf[32];
  281. X    WidgetClass class;
  282. X    Widget    parent;
  283. X
  284. X    va_start(var);
  285. X
  286. X    if (!(name = va_arg(var, char *)))
  287. X    name = GenericWidgetName(buf);
  288. X    class = va_arg(var, WidgetClass);
  289. X    parent = va_arg(var, Widget);
  290. X
  291. X    while (argstr = va_arg(var, char *)) {
  292. X    if (i == MAXARGS) {
  293. X        fprintf(stderr, "WidgetCreate: too many arguments: %d\n", i);
  294. X        err++;
  295. X        break;
  296. X    }
  297. X    if (!strcmp(argstr, XtNargList)) {
  298. X        ArgList list = va_arg(var, ArgList);
  299. X        int numargs = va_arg(var, int);
  300. X        for (numargs--; i < MAXARGS && numargs >= 0; i++, numargs--)
  301. X        XtSetArg(args[i], list[numargs].name, list[numargs].value);
  302. X        if (i == MAXARGS) {
  303. X        fprintf(stderr, "WidgetCreate: too many arguments: %d\n", i);
  304. X        err++;
  305. X        break;
  306. X        }
  307. X    } else if (!strcmp(argstr, XtNmanaged))
  308. X        /* managed is _really_ boolean, but varargs only works with ints */
  309. X        managed = va_arg(var, int);
  310. X    else {
  311. X        argval = va_arg(var, XtArgVal);
  312. X        XtSetArg(args[i], argstr, argval);
  313. X        ++i;
  314. X    }
  315. X    }
  316. X    va_end(var);
  317. X
  318. X    if (err)
  319. X    return NULL;
  320. X
  321. X    if (managed)
  322. X    return XtCreateManagedWidget(name, class, parent, args, i);
  323. X    else
  324. X    return XtCreateWidget(name, class, parent, args, i);
  325. X}
  326. X
  327. X/*
  328. X * WidgetSet()
  329. X *    Once a widget has been created, you may use this routine to
  330. X * add or change a varaible number of attributes on it.
  331. X */
  332. X/*VARARGS*/
  333. Xvoid
  334. XWidgetSet(va_alist)
  335. Xva_dcl
  336. X{
  337. X    String   argstr;
  338. X    Arg      args[MAXARGS];
  339. X    XtArgVal argval;
  340. X    int      i = 0;
  341. X    va_list  var;
  342. X    Widget   w;
  343. X
  344. X    va_start(var);
  345. X
  346. X    w = va_arg(var, Widget);
  347. X
  348. X    while (argstr = va_arg(var, char *)) {
  349. X    if (i == MAXARGS) {
  350. X        fprintf(stderr, "Warning: increase MAXARGS! (%d)\n", i);
  351. X        XtSetValues(w, args, i);
  352. X        i = 0;
  353. X    }
  354. X    if (!strcmp(argstr, XtNargList)) {
  355. X        ArgList list = va_arg(var, ArgList);
  356. X        XtArgVal numargs = va_arg(var, Cardinal);
  357. X        XtSetValues(w, list, numargs);
  358. X    } else {
  359. X        argval = va_arg(var, XtArgVal);
  360. X        XtSetArg(args[i], argstr, argval);
  361. X        ++i;
  362. X    }
  363. X    }
  364. X    va_end(var);
  365. X    if (i > 0)
  366. X    XtSetValues(w, args, i);
  367. X}
  368. X
  369. X/*
  370. X * WidgetGet()
  371. X *     Get the values of a widget via an interface identical to WidgetSet
  372. X */
  373. X/*VARARGS*/
  374. Xvoid
  375. XWidgetGet(va_alist)
  376. Xva_dcl
  377. X{
  378. X    String   argstr;
  379. X    Arg      args[MAXARGS];
  380. X    XtArgVal argval;
  381. X    int      i = 0;
  382. X    va_list  var;
  383. X    Widget   w;
  384. X
  385. X    va_start(var);
  386. X
  387. X    w = va_arg(var, Widget);
  388. X
  389. X    while (argstr = va_arg(var, char *)) {
  390. X    argval = va_arg(var, XtArgVal);
  391. X    if (i == MAXARGS) {
  392. X        fprintf(stderr, "Warning: increase MAXARGS! (%d)\n", i);
  393. X        XtGetValues(w, args, i);
  394. X        i = 0;
  395. X    }
  396. X    if (!strcmp(argstr, XtNargList)) {
  397. X        ArgList list = va_arg(var, ArgList);
  398. X        XtArgVal numargs = va_arg(var, Cardinal);
  399. X        XtGetValues(w, list, numargs);
  400. X    } else
  401. X        XtSetArg(args[i], argstr, argval);
  402. X        ++i;
  403. X    }
  404. X    va_end(var);
  405. X    if (i > 0)
  406. X    XtGetValues(w, args, i);
  407. X}
  408. +END+OF+WidgetWrap.c
  409. echo '-rw-r--r--  1 argv         6348 Oct 15 19:45 WidgetWrap.c    (as sent)'
  410. chmod u=rw,g=r,o=r WidgetWrap.c
  411. ls -l WidgetWrap.c
  412. echo x - WidgetWrap.h
  413. sed 's/^X//' > WidgetWrap.h <<'+END+OF+WidgetWrap.h'
  414. X/*
  415. X * WidgetWrap.h -- header file for the WidgetWrap library.
  416. X *
  417. X * This module was written by Dan Heller <island!argv@sun.com> or
  418. X * <dheller@ucbcory.berkeley.edu>.
  419. X */
  420. X
  421. X#define MAXARGS        50
  422. X#define XtNargList    "Arglist"
  423. X#define XtNmanaged    "Managed"
  424. X
  425. Xextern void WidgetSet(), WidgetGet();
  426. Xextern Widget WidgetCreate();
  427. Xextern char *GenericWidgetName();
  428. +END+OF+WidgetWrap.h
  429. echo '-rw-rw-r--  1 argv          352 Oct 15 19:45 WidgetWrap.h    (as sent)'
  430. chmod u=rw,g=rw,o=r WidgetWrap.h
  431. ls -l WidgetWrap.h
  432. echo x - Patchlevel.h
  433. sed 's/^X//' > Patchlevel.h <<'+END+OF+Patchlevel.h'
  434. X#define PATCHLEVEL 0
  435. +END+OF+Patchlevel.h
  436. echo '-rw-rw-r--  1 argv           21 Oct 15 19:47 Patchlevel.h    (as sent)'
  437. chmod u=rw,g=rw,o=r Patchlevel.h
  438. ls -l Patchlevel.h
  439. exit 0
  440. -- 
  441. Mike Wexler(wyse!mikew)    Phone: (408)433-1000 x1330
  442. Moderator of comp.sources.x
  443.